#include "VSResourceManager.h"
#include "VSGeometry.h"
#include "VSLight.h"
#include "VSMaterial.h"
#include "VSVShader.h"
#include "VSPShader.h"
#include "VSName.h"
#include "VSTexAllState.h"
#include "VSImage.h"
#include "VSShaderStringFactory.h"
#include "VSMatch.h"
#include "VSNVCompression.h"
#include "VS2DTexture.h"
#include "VSGraphicInclude.h"
#include "VSASYNLoader.h"
#include "VSRenderTarget.h"
#include "VSDepthStencil.h"
#include "VSCubeTexture.h"
#include "VSStream.h"
#include "VSPostEffectSet.h"
using namespace VSEngine2;
VSResouceControll::VSResouceControll()
{
}
VSResouceControll::~VSResouceControll()
{

}
void VSResouceControll::GCResource()
{

}
VSResourceDynamicBuffer::VSResourceDynamicBuffer()
{

}
VSResourceDynamicBuffer::~VSResourceDynamicBuffer()
{

}
void VSResourceDynamicBuffer::ClearInfo()
{
	for (unsigned int i= 0 ; i < m_ResourceArray.GetNum() ; i++)
	{
		m_ResourceArray[i]->ClearInfo();
	}
}
VSString VSResourceManager::ms_FileSuffix[] = 
{
	_T("TEXTURE"),
	_T("STMODEL"),
	_T("SKMODEL"),
	_T("ACTION"),
	_T("MATERIAL"),
	_T("ANIMSET"),
	_T("REFPOS"),
	_T("WEIGTHSET"),
	_T("POSTEFFECT"),
	_T("")


}; 
unsigned int VSResourceManager::ms_uiGpuSkinBoneNum = 50;
bool VSResourceManager::ms_bActiveDebugDraw = true;
bool VSResourceManager::ms_bDrawSkelecton = true;
bool VSResourceManager::ms_bRenderThread = false;
bool VSResourceManager::ms_bUpdateThread = false;
bool VSResourceManager::ms_bIsCacheName = false;
bool VSResourceManager::ms_bIsCacheShader = false;
bool VSResourceManager::ms_bIsCacheVertexFormat = false;
VSString VSResourceManager::ms_TexturePath;
VSString VSResourceManager::ms_AnimPath;
VSString VSResourceManager::ms_MaterialPath;
VSString VSResourceManager::ms_SkelectionMeshPath;
VSString VSResourceManager::ms_MorphPath;
VSString VSResourceManager::ms_OutputShaderCodePath;
VSString VSResourceManager::ms_OutPutLogPath;
VSString VSResourceManager::ms_ShaderPath;
VSString VSResourceManager::ms_StaticMeshPath;
VSString VSResourceManager::ms_TerrainPath;
VSString VSResourceManager::ms_PostEffectPath;
VSString VSResourceManager::ms_ResourcePath;
VSCriticalSection VSResourceManager::ms_TextureCri;
VSCriticalSection VSResourceManager::ms_MaterialCri ;
VSCriticalSection VSResourceManager::ms_AnimSetCri ;
VSCriticalSection VSResourceManager::ms_StaticMeshCri ;
VSCriticalSection VSResourceManager::ms_SkeletonMeshCri ;
VSCriticalSection VSResourceManager::ms_VShaderCri ;
VSCriticalSection VSResourceManager::ms_PShaderCri ;
VSCriticalSection VSResourceManager::ms_PostEffectCri ;
IMPLEMENT_PRIORITY(VSResourceManager)
static bool gs_bStreamRegistered_VSResourceManager= VSResourceManager::RegisterMainFactory ();
bool VSResourceManager::ms_bRegisterMainFactory = false;
bool VSResourceManager::RegisterMainFactory()
{
	if (!ms_bRegisterMainFactory)
	{ 


		if(!ms_Priority.AddPriorityThan(&VSFont::ms_Priority))
			return 0;

		if(!ms_Priority.AddPriorityThan(&VSVertexFormat::ms_Priority))
			return 0;

		if(!ms_Priority.AddPriorityThan(&VSVShader::ms_Priority))
			return 0;

		if(!ms_Priority.AddPriorityThan(&VSPShader::ms_Priority))
			return 0;
		VSMain::AddInitialFuntion(VSResourceManager::InitialDefaultState,&ms_Priority);
		ms_bRegisterMainFactory = true;
	} 
	return ms_bRegisterMainFactory;

}
bool VSResourceManager::InitialDefaultState()
{
	InitConfig();
	if (VSResourceManager::IsCacheShader())
	{
		InitCacheShader();
	}
	return 1;
}
VSResourceManager::VSResourceManager()
{
	
}
VSResourceManager::~VSResourceManager()
{

}
const VSString &VSResourceManager::GetFileSuffix(unsigned int uiFileSuffix)
{
	if(uiFileSuffix > FS_MAX)
		return ms_FileSuffix[FS_MAX];	
	return ms_FileSuffix[uiFileSuffix];
}
void VSResourceManager::InitConfig()
{
	VSMatch Match;
	if(!Match.Open(_T("Resource\\Config.txt")))
		return;

	//texture
	if (!Match.Getline())
	{
		VSMAC_ASSERT(0);
		return ;
	} // end if

	if (Match.PatternMatch("['TexturePath'][s>0]"))
	{
		ms_TexturePath = Match.pstrings[0];
	} // end if

	// anim
	if (!Match.Getline())
	{
		VSMAC_ASSERT(0);
		return ;
	} // end if

	if (Match.PatternMatch("['AnimPath'][s>0]"))
	{
		ms_AnimPath = Match.pstrings[0];
	} // end if

	//material
	if (!Match.Getline())
	{
		VSMAC_ASSERT(0);
		return ;
	} // end if

	if (Match.PatternMatch("['MaterialPath'][s>0]"))
	{
		ms_MaterialPath = Match.pstrings[0];
	} // end if
	
	//material effect

	if (!Match.Getline())
	{
		VSMAC_ASSERT(0);
		return ;
	} // end if

	if (Match.PatternMatch("['PostEffectPath'][s>0]"))
	{
		ms_PostEffectPath = Match.pstrings[0];
	} // end if
	
	//skelecton mesh
	if (!Match.Getline())
	{
		VSMAC_ASSERT(0);
		return ;
	} // end if

	if (Match.PatternMatch("['SKelectonMeshPath'][s>0]"))
	{
		ms_SkelectionMeshPath = Match.pstrings[0];
	} // end if

	//Morph
	if (!Match.Getline())
	{
		VSMAC_ASSERT(0);
		return ;
	} // end if

	if (Match.PatternMatch("['MorphPath'][s>0]"))
	{
		ms_MorphPath = Match.pstrings[0];
	} // end if

	//output
	if (!Match.Getline())
	{
		VSMAC_ASSERT(0);
		return ;
	} // end if

	if (Match.PatternMatch("['OutputShaderCodePath'][s>0]"))
	{
		ms_OutputShaderCodePath = Match.pstrings[0];
	} // end if

	if (!Match.Getline())
	{
		VSMAC_ASSERT(0);
		return ;
	} // end if

	if (Match.PatternMatch("['OutputLogPath'][s>0]"))
	{
		ms_OutPutLogPath = Match.pstrings[0];
	} // end if

	//Shader
	if (!Match.Getline())
	{
		VSMAC_ASSERT(0);
		return ;
	} // end if

	if (Match.PatternMatch("['ShaderPath'][s>0]"))
	{
		ms_ShaderPath = Match.pstrings[0];
	} // end if

	//StaticMesh
	if (!Match.Getline())
	{
		VSMAC_ASSERT(0);
		return ;
	} // end if

	if (Match.PatternMatch("['StaticMeshPath'][s>0]"))
	{
		ms_StaticMeshPath = Match.pstrings[0];
	} // end if

	//Terrain

	if (!Match.Getline())
	{
		VSMAC_ASSERT(0);
		return ;
	} // end if

	if (Match.PatternMatch("['TerrainPath'][s>0]"))
	{
		ms_TerrainPath = Match.pstrings[0];
	} // end if
	
	if (!Match.Getline())
	{
		VSMAC_ASSERT(0);
		return ;
	} // end if

	if (Match.PatternMatch("['ResourcePath'][s>0]"))
	{
		ms_ResourcePath = Match.pstrings[0];
	}
	
}
void VSResourceManager::AddDynamicBufferGeometry(VSDynamicBufferGeometry * pDBG)
{

	GetDynamicBufferGeometryArray().AddResource(pDBG);
}
void VSResourceManager::DeleteDynamicBufferGeometry(VSDynamicBufferGeometry * pDBG)
{
	GetDynamicBufferGeometryArray().DeleteResource(pDBG);
	
}
void VSResourceManager::ClearDynamicBufferGeometry()
{
	GetDynamicBufferGeometryArray().ClearInfo();
}
void VSResourceManager::InitCacheShader()
{
	VSStream Stream;
	VSString FileName = ms_ShaderPath + _T("CahceShader");
	if(!Stream.Load(FileName.GetBuffer()))
		return ;

}
bool VSResourceManager::CacheName()
{

	GetNameSet().ClearAll();
	return 1;
}
bool VSResourceManager::CacheVertexFormat()
{
	GetVertexFormatSet().ClearAll();
	return 1;
}
bool VSResourceManager::CacheShader()
{
	VSStream CacheStream;

	VSString FileName = ms_ShaderPath + _T("CahceShader");
	if(!CacheStream.Save(FileName.GetBuffer()))
		return 0;

	GetMaterialShaderMap().Clear();
	GetVertexShaderMap().Clear();
	GetPixelShaderMap().Clear();
	GetPostVertexShaderMap().Clear();
	GetPostPixelShaderMap().Clear();
	GetNormalDepthShaderMap().Clear();
	return 1;
}
void VSResourceManager::CacheResource()
{
	GetASYNSkelectonMeshSet().ClearAll();

	
	GetASYNStaticMeshSet().ClearAll();

	
	GetASYNMaterialSet().ClearAll();


	if (VSResourceManager::IsCacheShader())
	{
		CacheShader();

	}
	else
	{
		GetMaterialShaderMap().Clear();
		GetVertexShaderMap().Clear();
		GetPixelShaderMap().Clear();
		GetPostVertexShaderMap().Clear();
		GetPostPixelShaderMap().Clear();
		GetNormalDepthShaderMap().Clear();
	}
	GetASYNAnimSetSet().ClearAll();


	GetFontSet().ClearAll();

	
	if (VSResourceManager::IsCacheVertexFormat())
	{
		CacheVertexFormat();
	}
	else
	{
		GetVertexFormatSet().ClearAll();
	}
	GetBlendStateSet().ClearAll();
	GetDepthStencilStateSet().ClearAll();
	GetRasterizerStateSet().ClearAll();
	GetSamplerStateSet().ClearAll();

	GetASYNTextureSet().ClearAll();


	if (VSResourceManager::IsCacheName())
	{
		CacheName();
	}
	else
	{
		GetNameSet().ClearAll();
	}
	GetRenderTargetArray().ClearAll();
	GetDepthStencilArray().ClearAll();
	GetASYNPostEffectSet().ClearAll();
}

void VSResourceManager::GC()
{
	GetASYNTextureSet().GCResource();
	GetASYNAnimSetSet().GCResource();
	GetASYNMaterialSet().GCResource();
	GetASYNStaticMeshSet().GCResource();
	GetASYNSkelectonMeshSet().GCResource();


	GetRenderTargetArray().GCResource();
	GetDepthStencilArray().GCResource();
}
bool VSResourceManager::IsReleaseAll()
{
	if (!GetASYNStaticMeshSet().IsRealeseAll())
	{
		return false;
	}
	if (!GetASYNSkelectonMeshSet().IsRealeseAll())
	{
		return false;
	}
	
	if (!GetFontSet().IsRealeseAll())
	{
		return false;
	}
	if (!GetASYNTextureSet().IsRealeseAll())
	{
		return false;
	}
	if (!GetASYNTextureSet().IsRealeseAll())
	{
		return false;
	}
	if (!GetASYNMaterialSet().IsRealeseAll())
	{
		return false;
	}

	if (!GetVertexFormatSet().IsRealeseAll())
	{
		return false;
	}

	if (!GetMaterialShaderMap().IsRealeseAll())
	{
		return false;
	}

	if (!GetVertexShaderMap().IsRealeseAll())
	{
		return false;
	}

	if (!GetPixelShaderMap().IsRealeseAll())
	{
		return false;
	}
	if (!GetPostVertexShaderMap().IsRealeseAll())
	{
		return false;
	}
	if(!GetPostPixelShaderMap().IsRealeseAll())
	{
		return false;
	}
	if (!GetNameSet().IsRealeseAll())
	{
		return false;
	}
	
	if (!GetASYNAnimSetSet().IsRealeseAll())
	{
		return false;
	}
	
	if (!GetBlendStateSet().IsRealeseAll())
	{
		return false;
	}
	if (!GetSamplerStateSet().IsRealeseAll())
	{
		return false;
	}
	if (!GetDepthStencilStateSet().IsRealeseAll())
	{
		return false;
	}
	if (!GetRasterizerStateSet().IsRealeseAll())
	{
		return false;
	}
	if (!GetDynamicBufferGeometryArray().IsRealeseAll())
	{
		return false;
	}
	return true;
}
VSTexAllState * VSResourceManager::Load2DTexture(const TCHAR *pFileName,VSSamplerStatePtr pSamplerState)
{
	
	if (!pFileName)
	{
		
		return NULL;
	}
	VSFileName FileName = pFileName;
	VSString Extension; 

	if (!FileName.GetExtension(Extension))
	{
		
		
		return NULL;
	}
	VSTexAllState * pTexAllState = NULL;


	VSImage *pImage = NULL;

	if (Extension == VSImage::ms_ImageFormat[VSImage::IF_BMP])
	{
		pImage = VS_NEW VSBMPImage();
	}
	else if (Extension == VSImage::ms_ImageFormat[VSImage::IF_TGA])
	{
		pImage = VS_NEW VSTGAImage();
	}
	else 
	{
		
		return NULL;
	}
	if(!pImage->Load(FileName.GetBuffer()))
	{
		VSMAC_DELETE(pImage);
		
		return NULL;
	}

	unsigned int uiWidth = pImage->GetWidth();
	unsigned int uiHeight = pImage->GetHeight();
	if (!uiWidth || !uiHeight)
	{
		VSMAC_DELETE(pImage);
		
		return NULL;
	}
	if (!IsTwoPower(uiWidth) || !IsTwoPower(uiHeight))
	{
		VSMAC_DELETE(pImage);
		
		return NULL;
	}
	VS2DTexture * pTexture = NULL;
	//not all device R8G8B8 is available; 
	bool bIsHasAlpha = true;//(pImage->GetBPP() == 32);
	pTexture = VS_NEW VS2DTexture(uiWidth,uiHeight,bIsHasAlpha ? VSRenderer::SFT_A8R8G8B8 : VSRenderer::SFT_R8G8B8,0,1);
	if(!pTexture)
	{
		VSMAC_DELETE(pImage);
		
		return NULL;
	}
	pTexture->CreateRAMDate();
	for (unsigned int cy = 0; cy < uiHeight; cy++) 
	{
		for (unsigned int cx = 0; cx < uiWidth; cx++) 
		{

			unsigned uiIndex = cy * uiWidth + cx;
			unsigned char *pBuffer = pTexture->GetBuffer(0,uiIndex);
			const unsigned char * pImageBuffer = pImage->GetPixel(cx,cy);
			if (pImage->GetBPP() == 8)
			{
				pBuffer[0] = pImageBuffer[0];
				pBuffer[1] = pImageBuffer[0];
				pBuffer[2] = pImageBuffer[0];
				pBuffer[3] = 255;
			}
			else if (pImage->GetBPP() == 24)
			{
				pBuffer[0] = pImageBuffer[0];
				pBuffer[1] = pImageBuffer[1];
				pBuffer[2] = pImageBuffer[2];
				pBuffer[3] = 255;
			}
			else if (pImage->GetBPP() == 32)
			{
				pBuffer[0] = pImageBuffer[0];
				pBuffer[1] = pImageBuffer[1];
				pBuffer[2] = pImageBuffer[2];
				pBuffer[3] = pImageBuffer[3];
			}
		} // for
	} // for
	
	unsigned char * pLast = pTexture->GetBuffer(0);
	for (unsigned int i = 1 ; i < pTexture->GetMipLevel() ; i++)
	{
		unsigned char *pNow = pTexture->GetBuffer(i);

		if(!VSResourceManager::GetNextMipDate(pLast,pTexture->GetWidth(i - 1),pTexture->GetHeight(i - 1),pNow,pTexture->GetChannelPerPixel()))
		{
			VSMAC_DELETE(pTexture);
			VSMAC_DELETE(pImage);
			
			return NULL;
		}
		pLast = pTexture->GetBuffer(i);
	}
	if (pImage)
	{
		VSMAC_DELETE(pImage);
	}
	pTexAllState = VS_NEW VSTexAllState(pTexture);

	if (pSamplerState)
	{
		pTexAllState->SetSamplerState(pSamplerState);
	}
	
	
	return pTexAllState;
}
VSTexAllState * VSResourceManager::Load2DTextureCompress(const TCHAR *pFileName,VSSamplerStatePtr pSamplerState,
											 unsigned int uiCompressType,bool bIsNormal,bool bIsSoftSRGB)
{

	
	if (!pFileName)
	{
		
		return NULL;
	}
	
	if (uiCompressType > VSNVCompression::NV_MAX)
	{
		
		return NULL;
	}

	VSFileName FileName = pFileName;
	VSString Extension; 

	if (!FileName.GetExtension(Extension))
	{
		
		return NULL;
	}
	VSTexAllState * pTexAllState = NULL;

	VSImage *pImage = NULL;
	if (Extension == VSImage::ms_ImageFormat[VSImage::IF_BMP])
	{
		pImage = VS_NEW VSBMPImage();
	}
	else if (Extension == VSImage::ms_ImageFormat[VSImage::IF_TGA])
	{
		pImage = VS_NEW VSTGAImage();
	}
	else 
	{
		
		return NULL;
	}
	if(!pImage->Load(FileName.GetBuffer()))
	{
		VSMAC_DELETE(pImage);
		
		return NULL;
	}

	unsigned int uiWidth = pImage->GetWidth();
	unsigned int uiHeight = pImage->GetHeight();
	if (!uiWidth || !uiHeight)
	{
		VSMAC_DELETE(pImage);
		
		return NULL;
	}
	if (!IsTwoPower(uiWidth) || !IsTwoPower(uiHeight))
	{
		VSMAC_DELETE(pImage);
		
		return NULL;
	}
	VS2DTexture * pTexture = NULL;
	//not all device R8G8B8 is available; 
	bool bIsHasAlpha = true;//(pImage->GetBPP() == 32);
	pTexture = VS_NEW VS2DTexture(uiWidth,uiHeight,bIsHasAlpha ? VSRenderer::SFT_A8R8G8B8 : VSRenderer::SFT_R8G8B8,0,1);
	if(!pTexture)
	{
		VSMAC_DELETE(pImage);
		
		return NULL;
	}
	pTexture->CreateRAMDate();
	for (unsigned int cy = 0; cy < uiHeight; cy++) 
	{
		for (unsigned int cx = 0; cx < uiWidth; cx++) 
		{

			unsigned uiIndex = cy * uiWidth + cx;
			unsigned char *pBuffer = pTexture->GetBuffer(0,uiIndex);
			const unsigned char * pImageBuffer = pImage->GetPixel(cx,cy);
			if (pImage->GetBPP() == 8)
			{
				pBuffer[0] = pImageBuffer[0];
				pBuffer[1] = pImageBuffer[0];
				pBuffer[2] = pImageBuffer[0];
				pBuffer[3] = 255;
			}
			else if (pImage->GetBPP() == 24)
			{
				pBuffer[0] = pImageBuffer[0];
				pBuffer[1] = pImageBuffer[1];
				pBuffer[2] = pImageBuffer[2];
				pBuffer[3] = 255;
			}
			else if (pImage->GetBPP() == 32)
			{
				pBuffer[0] = pImageBuffer[0];
				pBuffer[1] = pImageBuffer[1];
				pBuffer[2] = pImageBuffer[2];
				pBuffer[3] = pImageBuffer[3];
			}
		} // for
	} // for

	unsigned char * pLast = pTexture->GetBuffer(0);
	for (unsigned int i = 1 ; i < pTexture->GetMipLevel() ; i++)
	{
		unsigned char *pNow = pTexture->GetBuffer(i);

		if(!VSResourceManager::GetNextMipDate(pLast,pTexture->GetWidth(i - 1),pTexture->GetHeight(i - 1),pNow,pTexture->GetChannelPerPixel()))
		{
			VSMAC_DELETE(pTexture);
			VSMAC_DELETE(pImage);
			
			return NULL;
		}
		pLast = pTexture->GetBuffer(i);
	}
	if (pImage)
	{
		VSMAC_DELETE(pImage);
	}

	unsigned int uiCompressFormat = VSNVCompression::NV_ARGB;
	unsigned int uiVSTextureTpye = VSRenderer::SFT_A8R8G8B8;
	if (uiCompressType == 0)
	{
		uiCompressFormat = VSNVCompression::NV_ARGB;
		uiVSTextureTpye = VSRenderer::SFT_A8R8G8B8;
	}
	else if (uiCompressType == 1)
	{
		uiCompressFormat = VSNVCompression::NV_DXT3;
		uiVSTextureTpye = VSRenderer::SFT_DXT3;
	}
	else if (uiCompressType == 2)
	{
		uiCompressFormat = VSNVCompression::NV_DXT5;
		uiVSTextureTpye = VSRenderer::SFT_DXT5;
	}
	VS2DTexture * pNewTexture = VS_NEW VS2DTexture(uiWidth,uiHeight,uiVSTextureTpye,0,1);
	pNewTexture->CreateRAMDate();
	for (unsigned int i = 0 ; i < pTexture->GetMipLevel() ; i++)
	{
		unsigned char *pNow = pTexture->GetBuffer(i);
		VSNVCompression Help;
		Help.Setup(pNow,uiCompressFormat,pTexture->GetWidth(i),pTexture->GetHeight(i),bIsSoftSRGB,bIsNormal);
		Help.Process();
		VSMemcpy(pNewTexture->GetBuffer(i),Help.OutputHandler.m_CompressDate.GetBuffer(),Help.OutputHandler.m_CompressDate.GetNum());
	}
	
	VSMAC_DELETE(pTexture);
	pTexAllState = VS_NEW VSTexAllState(pNewTexture);
	if (pSamplerState)
	{
		pTexAllState->SetSamplerState(pSamplerState);
	}
	
	return pTexAllState;
}

VSTexAllStateR * VSResourceManager::LoadASYN2DTexture(const TCHAR *pFileName,bool IsAsyn,VSSamplerStatePtr pSamplerState)
{
	if (!pFileName)
	{
		return NULL;
	}
	
	VSFileName FileName = pFileName;
	VSString Extension; 
	VSUsedName ResourceName;
	if (FileName.GetExtension(Extension))
	{
		if (Extension != VSImage::ms_ImageFormat[VSImage::IF_BMP] && Extension != VSImage::ms_ImageFormat[VSImage::IF_TGA])
		{
			return NULL;
		}
		else
		{
			ResourceName = FileName;
			FileName =  ms_TexturePath + FileName;
			
		}
	}
	else
	{
		return NULL;
	}
	ms_TextureCri.Lock();
	VSTexAllStateRPtr pTexAllState = NULL;
	pTexAllState = (VSTexAllStateR *)VSResourceManager::GetASYNTextureSet().CheckIsHaveTheResource(ResourceName);
	if(pTexAllState)
	{
		ms_TextureCri.Unlock();	
		return pTexAllState;
	}
	pTexAllState = VS_NEW VSTexAllStateR();

	pTexAllState->SetResourceName(ResourceName);


	VSResourceManager::GetASYNTextureSet().AddResource(ResourceName,pTexAllState);

	
	if (IsAsyn)
	{
		VSMAC_ASSERT(VSASYNLoadManager::ms_pASYNLoadManager);
		VSASYNLoadManager::ms_pASYNLoadManager->AddTextureLoad(pTexAllState,FileName,false,pSamplerState);
	}
	else
	{	
		VSTexAllState * pTex = Load2DTexture(FileName.GetBuffer(),pSamplerState);
		pTexAllState->SetNewResource(pTex);
		pTexAllState->Loaded();
	}
	ms_TextureCri.Unlock();
	return pTexAllState;
}
//0 no compress 1 dx3  2 dx5
VSTexAllStateR * VSResourceManager::LoadASYN2DTextureCompress(const TCHAR *pFileName,bool IsAsyn,VSSamplerStatePtr pSamplerState,
												  unsigned int uiCompressType,bool bIsNormal,bool bIsSoftSRGB)
{
	if (!pFileName)
	{
		return NULL;
	}
	if (uiCompressType > VSNVCompression::NV_MAX)
	{
		return NULL;
	}
	VSFileName FileName = pFileName;
	VSString Extension; 
	VSUsedName ResourceName;
	if (FileName.GetExtension(Extension))
	{
		if (Extension != VSImage::ms_ImageFormat[VSImage::IF_BMP] && Extension != VSImage::ms_ImageFormat[VSImage::IF_TGA])
		{
			return NULL;
		}
		else
		{
			ResourceName = FileName;
			FileName =  ms_TexturePath + FileName;
			
		}
	}
	else
	{
		return NULL;
	}
	ms_TextureCri.Lock();
	VSTexAllStateRPtr pTexAllState = NULL;
	pTexAllState = (VSTexAllStateR *)VSResourceManager::GetASYNTextureSet().CheckIsHaveTheResource(ResourceName);
	if(pTexAllState)
	{
		ms_TextureCri.Unlock();
		return pTexAllState;
	}
	pTexAllState = VS_NEW VSTexAllStateR();

	pTexAllState->SetResourceName(ResourceName);


	VSResourceManager::GetASYNTextureSet().AddResource(ResourceName,pTexAllState);

	if (IsAsyn)
	{
		VSMAC_ASSERT(VSASYNLoadManager::ms_pASYNLoadManager);
		VSASYNLoadManager::ms_pASYNLoadManager->AddTextureLoad(pTexAllState,FileName,false,pSamplerState,true,uiCompressType,bIsNormal,bIsSoftSRGB);
	}
	else
	{	
		VSTexAllState * pTex = Load2DTextureCompress(FileName.GetBuffer(),pSamplerState,uiCompressType,bIsNormal,bIsSoftSRGB);
		pTexAllState->SetNewResource(pTex);
		pTexAllState->Loaded();
	}
	ms_TextureCri.Unlock();
	return pTexAllState;
}
VSResourceProxyBase * VSResourceManager::LoadResource(const TCHAR *pFileName,bool IsAsyn)
{
	VSFileName FileName = pFileName;
	VSString Extension; 
	FileName.GetExtension(Extension);
	VSResourceProxyBase * pResource = NULL;
	if (Extension == ms_FileSuffix[FS_TEXTURE])
	{
		pResource = NewLoadASYNTexture(pFileName,IsAsyn);
	}
	else if (Extension == ms_FileSuffix[FS_MATERIAL])
	{
		pResource = NewLoadASYNMaterial(pFileName,IsAsyn);
	}
	else if (Extension == ms_FileSuffix[FS_ANIMSET])
	{
		pResource = LoadASYNAnimSet(pFileName,IsAsyn);
	}
	else if (Extension == ms_FileSuffix[FS_STATIC_MODEL])
	{
		pResource = LoadASYNStaticMesh(pFileName,IsAsyn);
	}
	else if (Extension == ms_FileSuffix[FS_SKELECTON_MODEL])
	{
		pResource = LoadASYNSkelectonMesh(pFileName,IsAsyn);
	}
	return pResource;
}
VSTexAllStateR * VSResourceManager::LoadASYNTexture(const TCHAR *pFileName,bool IsAsyn)
{
	if (!pFileName)
	{
		return NULL;
	}
	VSFileName FileName = pFileName;
	VSString Extension; 
	VSUsedName ResourceName;
	if (FileName.GetExtension(Extension))
	{
		if (Extension != ms_FileSuffix[FS_TEXTURE])
		{
			return NULL;
		}
		else
		{
			ResourceName = FileName;
			FileName =  ms_TexturePath + FileName;
			
		}
	}
	else
	{		
		ResourceName = FileName + _T(".") + ms_FileSuffix[FS_TEXTURE];
		FileName =  ms_TexturePath + FileName + _T(".") + ms_FileSuffix[FS_TEXTURE];
		
	}
	ms_TextureCri.Lock();
	VSTexAllStateRPtr pTexAllState = NULL;
	pTexAllState = (VSTexAllStateR *)VSResourceManager::GetASYNTextureSet().CheckIsHaveTheResource(ResourceName);
	if(pTexAllState)
	{
		ms_TextureCri.Unlock();
		return pTexAllState;
	}
	pTexAllState = VS_NEW VSTexAllStateR();

	pTexAllState->SetResourceName(ResourceName);


	VSResourceManager::GetASYNTextureSet().AddResource(ResourceName,pTexAllState);

	
	if (IsAsyn && pTexAllState->IsEndableASYNLoad())
	{
		VSMAC_ASSERT(VSASYNLoadManager::ms_pASYNLoadManager);
		VSASYNLoadManager::ms_pASYNLoadManager->AddTextureLoad(pTexAllState,FileName);
	}
	else
	{	
		VSTexAllState * pTex = LoadTexture(FileName.GetBuffer());
		pTexAllState->SetNewResource(pTex);
		pTexAllState->Loaded();
	}
	ms_TextureCri.Unlock();
	return pTexAllState;
	
}
VSTexAllStateR * VSResourceManager::NewLoadASYNTexture(const TCHAR *pFileName,bool IsAsyn)
{
	if (!pFileName)
	{
		return NULL;
	}
	VSFileName FileName = pFileName;
	VSString Extension; 
	VSUsedName ResourceName;
	if (FileName.GetExtension(Extension))
	{
		if (Extension != ms_FileSuffix[FS_TEXTURE])
		{
			return NULL;
		}
		else
		{
			ResourceName = FileName;
			FileName =  ms_TexturePath + FileName;

		}
	}
	else
	{		
		ResourceName = FileName + _T(".") + ms_FileSuffix[FS_TEXTURE];
		FileName =  ms_TexturePath + FileName + _T(".") + ms_FileSuffix[FS_TEXTURE];

	}
	ms_TextureCri.Lock();
	VSTexAllStateRPtr pTexAllState = NULL;
	pTexAllState = (VSTexAllStateR *)VSResourceManager::GetASYNTextureSet().CheckIsHaveTheResource(ResourceName);
	if(pTexAllState)
	{
		ms_TextureCri.Unlock();
		return pTexAllState;
	}
	pTexAllState = VS_NEW VSTexAllStateR();

	pTexAllState->SetResourceName(ResourceName);


	VSResourceManager::GetASYNTextureSet().AddResource(ResourceName,pTexAllState);


	if (IsAsyn && pTexAllState->IsEndableASYNLoad())
	{
		VSMAC_ASSERT(VSASYNLoadManager::ms_pASYNLoadManager);
		VSASYNLoadManager::ms_pASYNLoadManager->AddTextureLoad(pTexAllState,FileName);
	}
	else
	{	
		VSTexAllState * pTex = NewLoadTexture(FileName.GetBuffer());
		pTexAllState->SetNewResource(pTex);
		pTexAllState->Loaded();
	}
	ms_TextureCri.Unlock();
	return pTexAllState;

}
VSMaterialR * VSResourceManager::NewLoadASYNMaterial(const TCHAR *pFileName,bool IsAsyn)
{
	if (!pFileName)
	{
		return NULL;
	}
	VSFileName FileName = pFileName;
	VSString Extension; 
	VSUsedName ResourceName;
	if (FileName.GetExtension(Extension))
	{
		if (Extension != ms_FileSuffix[FS_MATERIAL])
		{
			return NULL;
		}
		else
		{
			ResourceName = FileName;
			FileName =  ms_MaterialPath + FileName;

		}
	}
	else
	{		
		ResourceName = FileName + _T(".") + ms_FileSuffix[FS_MATERIAL];
		FileName =  ms_MaterialPath + FileName + _T(".") + ms_FileSuffix[FS_MATERIAL];

	}
	ms_MaterialCri.Lock();
	VSMaterialRPtr pMaterialR = NULL;
	pMaterialR = (VSMaterialR *)VSResourceManager::GetASYNMaterialSet().CheckIsHaveTheResource(ResourceName);
	if(pMaterialR)
	{
		ms_MaterialCri.Unlock();
		return pMaterialR;
	}
	pMaterialR = VS_NEW VSMaterialR();

	pMaterialR->SetResourceName(ResourceName);


	VSResourceManager::GetASYNMaterialSet().AddResource(ResourceName,pMaterialR);


	if (IsAsyn && pMaterialR->IsEndableASYNLoad())
	{
		VSMAC_ASSERT(VSASYNLoadManager::ms_pASYNLoadManager);
		VSASYNLoadManager::ms_pASYNLoadManager->AddMaterial(pMaterialR,FileName);
	}
	else
	{	
		VSMaterial * pMaterial = NewLoadMaterial(FileName.GetBuffer());
		pMaterialR->SetNewResource(pMaterial);
		pMaterialR->Loaded();
	}
	ms_MaterialCri.Unlock();
	return pMaterialR;
}
VSStaticMeshNodeR * VSResourceManager::LoadASYNStaticMesh(const TCHAR *pFileName,bool IsAsyn)
{
	if (!pFileName)
	{
		return NULL;
	}
	VSFileName FileName = pFileName;
	VSString Extension; 
	VSUsedName ResourceName;
	if (FileName.GetExtension(Extension))
	{
		if (Extension != ms_FileSuffix[FS_STATIC_MODEL])
		{
			return NULL;
		}
		else
		{
			ResourceName = FileName;
			FileName =  ms_StaticMeshPath + FileName;

		}
	}
	else
	{		
		ResourceName = FileName + _T(".") + ms_FileSuffix[FS_STATIC_MODEL];
		FileName =  ms_StaticMeshPath + FileName + _T(".") + ms_FileSuffix[FS_STATIC_MODEL];

	}
	ms_StaticMeshCri.Lock();
	VSStaticMeshNodeRPtr pStaticMeshNodeR = NULL;
	pStaticMeshNodeR = (VSStaticMeshNodeR *)VSResourceManager::GetASYNStaticMeshSet().CheckIsHaveTheResource(ResourceName);
	if(pStaticMeshNodeR)
	{
		ms_StaticMeshCri.Unlock();	
		return pStaticMeshNodeR;
	}
	pStaticMeshNodeR = VS_NEW VSStaticMeshNodeR();

	pStaticMeshNodeR->SetResourceName(ResourceName);


	VSResourceManager::GetASYNStaticMeshSet().AddResource(ResourceName,pStaticMeshNodeR);


	if (IsAsyn && pStaticMeshNodeR->IsEndableASYNLoad())
	{
		VSMAC_ASSERT(VSASYNLoadManager::ms_pASYNLoadManager);
		VSASYNLoadManager::ms_pASYNLoadManager->AddStaticMesh(pStaticMeshNodeR,FileName);
	}
	else
	{	
		VSStaticMeshNode * pStaticMeshNode = LoadStaticMesh(FileName.GetBuffer());
		pStaticMeshNodeR->SetNewResource(pStaticMeshNode);
		pStaticMeshNodeR->Loaded();
	}
	ms_StaticMeshCri.Unlock();	
	return pStaticMeshNodeR;
}
VSVShaderR * VSResourceManager::LoadVShader(MaterialShaderPara1 &MSPara1,unsigned int uiPassType,unsigned int uiShaderID,bool IsAsyn)
{
	VSVShaderRPtr pVShaderR = NULL;
	pVShaderR = VS_NEW VSVShaderR();
	if (IsAsyn && pVShaderR->IsEndableASYNLoad())
	{
		VSMAC_ASSERT(VSASYNLoadManager::ms_pASYNLoadManager);
		VSASYNLoadManager::ms_pASYNLoadManager->AddVShader(pVShaderR,MSPara1,uiPassType,uiShaderID);
	}
	else
	{
		VSVShader * pVShader = CreateVShader(MSPara1,uiPassType,uiShaderID);
		pVShaderR->SetNewResource(pVShader);
		pVShaderR->Loaded();
	}
	return pVShaderR;
}
VSPShaderR * VSResourceManager::LoadPShader(MaterialShaderPara1 &MSPara1,unsigned int uiPassType,unsigned int uiShaderID,bool IsAsyn)
{
	VSPShaderRPtr pPShaderR = NULL;
	pPShaderR = VS_NEW VSPShaderR();
	if (IsAsyn && pPShaderR->IsEndableASYNLoad())
	{
		VSMAC_ASSERT(VSASYNLoadManager::ms_pASYNLoadManager);
		VSASYNLoadManager::ms_pASYNLoadManager->AddPShader(pPShaderR,MSPara1,uiPassType,uiShaderID);
	}
	else
	{
		VSPShader * pPShader = CreatePShader(MSPara1,uiPassType,uiShaderID);
		pPShaderR->SetNewResource(pPShader);
		pPShaderR->Loaded();
	}
	return pPShaderR;
}
VSSkelectonMeshNodeR * VSResourceManager::LoadASYNSkelectonMesh(const TCHAR *pFileName,bool IsAsyn)
{
	if (!pFileName)
	{
		return NULL;
	}
	VSFileName FileName = pFileName;
	VSString Extension; 
	VSUsedName ResourceName;
	if (FileName.GetExtension(Extension))
	{
		if (Extension != ms_FileSuffix[FS_SKELECTON_MODEL])
		{
			return NULL;
		}
		else
		{
			ResourceName = FileName;
			FileName =  ms_SkelectionMeshPath + FileName;

		}
	}
	else
	{		
		ResourceName = FileName + _T(".") + ms_FileSuffix[FS_SKELECTON_MODEL];
		FileName =  ms_SkelectionMeshPath + FileName + _T(".") + ms_FileSuffix[FS_SKELECTON_MODEL];

	}
	ms_SkeletonMeshCri.Lock();
	VSSkelectonMeshNodeRPtr pSkelectonMeshNodeR = NULL;
	pSkelectonMeshNodeR = (VSSkelectonMeshNodeR *)VSResourceManager::GetASYNSkelectonMeshSet().CheckIsHaveTheResource(ResourceName);
	if(pSkelectonMeshNodeR)
	{
		ms_SkeletonMeshCri.Unlock();
		return pSkelectonMeshNodeR;
	}
	pSkelectonMeshNodeR = VS_NEW VSSkelectonMeshNodeR();

	pSkelectonMeshNodeR->SetResourceName(ResourceName);


	VSResourceManager::GetASYNSkelectonMeshSet().AddResource(ResourceName,pSkelectonMeshNodeR);


	if (IsAsyn && pSkelectonMeshNodeR->IsEndableASYNLoad())
	{
		VSMAC_ASSERT(VSASYNLoadManager::ms_pASYNLoadManager);
		VSASYNLoadManager::ms_pASYNLoadManager->AddSkelectonMesh(pSkelectonMeshNodeR,FileName);
	}
	else
	{	
		VSSkelectonMeshNode * pSkelectonMesh = LoadSkelectonMesh(FileName.GetBuffer());
		pSkelectonMeshNodeR->SetNewResource(pSkelectonMesh);
		pSkelectonMeshNodeR->Loaded();
	}
	ms_SkeletonMeshCri.Unlock();
	return pSkelectonMeshNodeR;
}
VSPostEffectSetR * VSResourceManager::LoadASYNPostEffect(const TCHAR *pFileName,bool IsAsyn)
{
	if (!pFileName)
	{
		return NULL;
	}
	VSFileName FileName = pFileName;
	VSString Extension; 
	VSUsedName ResourceName;
	if (FileName.GetExtension(Extension))
	{
		if (Extension != ms_FileSuffix[FS_POSTEFFECT])
		{
			return NULL;
		}
		else
		{
			ResourceName = FileName;
			FileName =  ms_PostEffectPath + FileName;

		}
	}
	else
	{		
		ResourceName = FileName + _T(".") + ms_FileSuffix[FS_POSTEFFECT];
		FileName =  ms_PostEffectPath + FileName + _T(".") + ms_FileSuffix[FS_POSTEFFECT];

	}
	ms_PostEffectCri.Lock();
	VSPostEffectSetRPtr pPostEffectSetR = NULL;
	pPostEffectSetR = (VSPostEffectSetR *)VSResourceManager::GetASYNPostEffectSet().CheckIsHaveTheResource(ResourceName);
	if(pPostEffectSetR)
	{
		ms_PostEffectCri.Unlock();
		return pPostEffectSetR;
	}
	pPostEffectSetR = VS_NEW VSPostEffectSetR();

	pPostEffectSetR->SetResourceName(ResourceName);


	VSResourceManager::GetASYNPostEffectSet().AddResource(ResourceName,pPostEffectSetR);


	if (IsAsyn && pPostEffectSetR->IsEndableASYNLoad())
	{
		VSMAC_ASSERT(VSASYNLoadManager::ms_pASYNLoadManager);
		//VSASYNLoadManager::ms_pASYNLoadManager->AddSkelectonMesh(pPostEffectSetR,FileName);
	}
	else
	{	
		VSPostEffectSet * pPostEffectSet = NewLoadPostEffect(FileName.GetBuffer());
		pPostEffectSetR->SetNewResource(pPostEffectSet);
		pPostEffectSetR->Loaded();
	}
	ms_PostEffectCri.Unlock();
	return pPostEffectSetR;
}
VSAnimSetR * VSResourceManager::LoadASYNAnimSet(const TCHAR *pFileName,bool IsAsyn)
{
	if (!pFileName)
	{
		return NULL;
	}
	VSFileName FileName = pFileName;
	VSString Extension; 
	VSUsedName ResourceName;
	if (FileName.GetExtension(Extension))
	{
		if (Extension != ms_FileSuffix[FS_ANIMSET])
		{
			return NULL;
		}
		else
		{
			ResourceName = FileName;
			FileName =  ms_AnimPath + FileName;

		}
	}
	else
	{		
		ResourceName = FileName + _T(".") + ms_FileSuffix[FS_ANIMSET];
		FileName =  ms_AnimPath + FileName + _T(".") + ms_FileSuffix[FS_ANIMSET];

	}
	ms_AnimSetCri.Lock();
	VSAnimSetRPtr pAnimSetR = NULL;
	pAnimSetR = (VSAnimSetR *)VSResourceManager::GetASYNAnimSetSet().CheckIsHaveTheResource(ResourceName);
	if(pAnimSetR)
	{
		ms_AnimSetCri.Unlock();
		return pAnimSetR;
	}
	pAnimSetR = VS_NEW VSAnimSetR();

	pAnimSetR->SetResourceName(ResourceName);


	VSResourceManager::GetASYNAnimSetSet().AddResource(ResourceName,pAnimSetR);


	if (IsAsyn && pAnimSetR->IsEndableASYNLoad() )
	{
		VSMAC_ASSERT(VSASYNLoadManager::ms_pASYNLoadManager);
		VSASYNLoadManager::ms_pASYNLoadManager->AddAnimSet(pAnimSetR,FileName);
	}
	else
	{	
		VSAnimSet * pAnimSet = LoadAnimSet(FileName.GetBuffer());
		pAnimSetR->SetNewResource(pAnimSet);
		pAnimSetR->Loaded();
	}
	ms_AnimSetCri.Unlock();
	return pAnimSetR;
}
VSMaterialR * VSResourceManager::LoadASYNMaterial(const TCHAR *pFileName,bool IsAsyn)
{
	if (!pFileName)
	{
		return NULL;
	}
	VSFileName FileName = pFileName;
	VSString Extension; 
	VSUsedName ResourceName;
	if (FileName.GetExtension(Extension))
	{
		if (Extension != ms_FileSuffix[FS_MATERIAL])
		{
			return NULL;
		}
		else
		{
			ResourceName = FileName;
			FileName =  ms_MaterialPath + FileName;

		}
	}
	else
	{		
		ResourceName = FileName + _T(".") + ms_FileSuffix[FS_MATERIAL];
		FileName =  ms_MaterialPath + FileName + _T(".") + ms_FileSuffix[FS_MATERIAL];

	}
	ms_MaterialCri.Lock();
	VSMaterialRPtr pMaterialR = NULL;
	pMaterialR = (VSMaterialR *)VSResourceManager::GetASYNMaterialSet().CheckIsHaveTheResource(ResourceName);
	if(pMaterialR)
	{
		ms_MaterialCri.Unlock();
		return pMaterialR;
	}
	pMaterialR = VS_NEW VSMaterialR();

	pMaterialR->SetResourceName(ResourceName);


	VSResourceManager::GetASYNMaterialSet().AddResource(ResourceName,pMaterialR);


	if (IsAsyn && pMaterialR->IsEndableASYNLoad())
	{
		VSMAC_ASSERT(VSASYNLoadManager::ms_pASYNLoadManager);
		VSASYNLoadManager::ms_pASYNLoadManager->AddMaterial(pMaterialR,FileName);
	}
	else
	{	
		VSMaterial * pMaterial = LoadMaterial(FileName.GetBuffer());
		pMaterialR->SetNewResource(pMaterial);
		pMaterialR->Loaded();
	}
	ms_MaterialCri.Unlock();
	return pMaterialR;
}
VSTexAllState * VSResourceManager::LoadTexture(const TCHAR *pFileName)
{

	
	if (!pFileName)
	{
		
		return NULL;
	}

	
	VSTexAllState * pTexAllState = NULL;


	VSStream Stream;
	if(!Stream.Load(pFileName))
	{
		
		return NULL;
	}

	pTexAllState = (VSTexAllState *)Stream.GetObjectByRtti(VSTexAllState::ms_Type);

	if (!pTexAllState)
	{
		VSMAC_DELETE(pTexAllState);
		
		return NULL;
	}
	if(!pTexAllState->m_pTex)
	{
		VSMAC_DELETE(pTexAllState);
		
		return NULL;
	}


	
	return pTexAllState;

}
VSTexAllState * VSResourceManager::NewLoadTexture(const TCHAR *pFileName)
{
	
	if (!pFileName)
	{
		
		return NULL;
	}
	VSStream LoadStream;
	LoadStream.NewLoad(pFileName);
	VSTexAllState * pTexAllState = (VSTexAllState *)LoadStream.GetObjectByRtti(VSTexAllState::ms_Type);
	if (!pTexAllState)
	{
		VSMAC_DELETE(pTexAllState);
		
		return NULL;
	}
	if(!pTexAllState->m_pTex)
	{
		VSMAC_DELETE(pTexAllState);
		
		return NULL;
	}
	
	return pTexAllState;

}

bool VSResourceManager::NewSaveTexture(VSTexAllState * pTexture,TCHAR * PathName)
{
	if (!pTexture || !pTexture->m_pTex || !pTexture->m_pTex->IsStatic())
	{
		return false;
	}

	VSStream SaveStream;
	SaveStream.SetStreamFlag(VSStream::AT_REGISTER);
	SaveStream.ArchiveAll(pTexture);
	VSString FileName = ms_TexturePath + PathName;
	FileName += _T(".") + ms_FileSuffix[FS_TEXTURE];
	return SaveStream.NewSave(FileName.GetBuffer());
}
bool VSResourceManager::SaveTexture(VSTexAllState * pTexture)
{
	if (!pTexture || !pTexture->m_pTex || !pTexture->m_pTex->IsStatic())
	{
		return false;
	}
	VSString FileName;

	if (!pTexture->GetResourceName().GetLength())
	{
		return 0;
	}
	FileName = ms_TexturePath + pTexture->GetResourceName().GetString();
	VSStream SaveStream;
	if(!pTexture->Register(SaveStream))
		return 0;
	FileName += _T(".") + ms_FileSuffix[FS_TEXTURE];
	if(!SaveStream.Save(FileName.GetBuffer()))
		return 0;
	return 1;
}
VSMaterial * VSResourceManager::NewLoadMaterial(const TCHAR *pFileName)
{
	
	if (!pFileName)
	{
		
		return NULL;
	}
	VSStream LoadStream;
	LoadStream.NewLoad(pFileName);
	VSMaterial * pMaterial = (VSMaterial *)LoadStream.GetObjectByRtti(VSMaterial::ms_Type);
	if (!pMaterial)
	{
		VSMAC_DELETE(pMaterial);
		
		return NULL;
	}
	
	return pMaterial;
}
bool VSResourceManager::NewSaveMaterial(VSMaterial * pMaterial,TCHAR * PathName)
{
	if (!pMaterial)
	{
		return false;
	}

	VSStream SaveStream;
	SaveStream.SetStreamFlag(VSStream::AT_REGISTER);
	SaveStream.ArchiveAll(pMaterial);
	VSString FileName = ms_MaterialPath + PathName;
	FileName += _T(".") + ms_FileSuffix[FS_MATERIAL];
	return SaveStream.NewSave(FileName.GetBuffer());
}
VSMaterial * VSResourceManager::LoadMaterial(const TCHAR *pFileName)
{
	
	if (!pFileName)
	{
		
		return NULL;
	}

	VSMaterial * pMaterial = NULL;


	VSStream Stream;
	if(!Stream.Load(pFileName))
	{
		
		return NULL;
	}

	pMaterial = (VSMaterial *)Stream.GetObjectByRtti(VSMaterial::ms_Type);

	if (!pMaterial)
	{
		VSMAC_DELETE(pMaterial);
		
		return NULL;
	}

	
	return pMaterial;
}
bool VSResourceManager::SaveMaterial(VSMaterial * pMaterial)
{
	if (!pMaterial)
	{
		return false;
	}
	if (!pMaterial->GetResourceName().GetLength())
	{
		return false;
	}
	VSString FileName = ms_MaterialPath + pMaterial->GetResourceName().GetString();
	pMaterial->LinkAllResource();
	VSStream MaterialStream;
	if(!pMaterial->Register(MaterialStream))
		return 0;

	FileName += _T(".") + ms_FileSuffix[FS_MATERIAL];
	if(!MaterialStream.Save(FileName.GetBuffer()))
		return 0;
	return 1;
}
VSAnimSet * VSResourceManager::NewLoadAnimSet(const TCHAR *pFileName)
{
	
	if (!pFileName)
	{
		
		return NULL;
	}
	VSStream LoadStream;
	LoadStream.NewLoad(pFileName);
	VSAnimSet * pAnimSet = (VSAnimSet *)LoadStream.GetObjectByRtti(VSAnimSet::ms_Type);
	if (!pAnimSet)
	{
		VSMAC_DELETE(pAnimSet);
		
		return NULL;
	}
	
	return pAnimSet;
}
bool VSResourceManager::NewSaveAnimSet(VSAnimSet * pAnimSet)
{
	if (!pAnimSet)
	{
		return false;
	}
	VSString FileName;

	if (!pAnimSet->GetResourceName().GetLength())
	{
		return 0;
	}
	FileName = ms_AnimPath + pAnimSet->GetResourceName().GetString();
	VSStream SaveStream;
	if(!pAnimSet->Register(SaveStream))
		return 0;
	FileName += _T(".") + ms_FileSuffix[FS_ANIMSET];
	if(!SaveStream.Save(FileName.GetBuffer()))
		return 0;
	return 1;
}
VSStaticMeshNode * VSResourceManager::NewLoadStaticMesh(const TCHAR *pFileName)
{
	
	if (!pFileName)
	{
		
		return NULL;
	}
	VSStream LoadStream;
	LoadStream.NewLoad(pFileName);
	VSStaticMeshNode * pStaticMesh = (VSStaticMeshNode *)LoadStream.GetObjectByRtti(VSStaticMeshNode::ms_Type);
	if (!pStaticMesh)
	{
		VSMAC_DELETE(pStaticMesh);
		
		return NULL;
	}
	
	return pStaticMesh;
}
bool VSResourceManager::NewSaveStaticMesh(VSStaticMeshNode * pStaticMeshNode)
{
	if (!pStaticMeshNode)
	{
		return false;
	}
	VSString FileName;

	if (!pStaticMeshNode->GetResourceName().GetLength())
	{
		return 0;
	}
	FileName = ms_StaticMeshPath + pStaticMeshNode->GetResourceName().GetString();
	VSStream SaveStream;
	if(!pStaticMeshNode->Register(SaveStream))
		return 0;
	FileName += _T(".") + ms_FileSuffix[FS_STATIC_MODEL];
	if(!SaveStream.Save(FileName.GetBuffer()))
		return 0;
	return 1;
}
VSPostEffectSet * VSResourceManager::NewLoadPostEffect(const TCHAR *pFileName)
{
	if (!pFileName)
	{
		return NULL;
	}
	VSStream LoadStream;
	LoadStream.NewLoad(pFileName);
	VSPostEffectSet * pPostEffectSet = (VSPostEffectSet *)LoadStream.GetObjectByRtti(VSPostEffectSet::ms_Type);
	if (!pPostEffectSet)
	{
		VSMAC_DELETE(pPostEffectSet);
		return NULL;
	}
	return pPostEffectSet;
}
bool VSResourceManager::NewSavePostEffect(VSPostEffectSet * pPostEffectSet)
{
	if (!pPostEffectSet)
	{
		return false;
	}
	VSString FileName;

	if (!pPostEffectSet->GetResourceName().GetLength())
	{
		return 0;
	}
	FileName = ms_SkelectionMeshPath + pPostEffectSet->GetResourceName().GetString();
	VSStream SaveStream;
	if(!pPostEffectSet->Register(SaveStream))
		return 0;
	FileName += _T(".") + ms_FileSuffix[FS_POSTEFFECT];
	if(!SaveStream.Save(FileName.GetBuffer()))
		return 0;
	return 1;	
}
VSSkelectonMeshNode * VSResourceManager::NewLoadSkelectonMesh(const TCHAR *pFileName)
{
	
	if (!pFileName)
	{
		
		return NULL;
	}
	VSStream LoadStream;
	LoadStream.NewLoad(pFileName);
	VSSkelectonMeshNode * pSkelecton = (VSSkelectonMeshNode *)LoadStream.GetObjectByRtti(VSSkelectonMeshNode::ms_Type);
	if (!pSkelecton)
	{
		VSMAC_DELETE(pSkelecton);
		
		return NULL;
	}
	
	return pSkelecton;
}
bool VSResourceManager::NewSaveSkelectonMeshNode(VSSkelectonMeshNode * pSkelectonMesh)
{
	if (!pSkelectonMesh)
	{
		return false;
	}
	VSString FileName;

	if (!pSkelectonMesh->GetResourceName().GetLength())
	{
		return 0;
	}
	FileName = ms_SkelectionMeshPath + pSkelectonMesh->GetResourceName().GetString();
	VSStream SaveStream;
	if(!pSkelectonMesh->Register(SaveStream))
		return 0;
	FileName += _T(".") + ms_FileSuffix[FS_SKELECTON_MODEL];
	if(!SaveStream.Save(FileName.GetBuffer()))
		return 0;
	return 1;
}
VSAnimSet * VSResourceManager::LoadAnimSet(const TCHAR *pFileName)
{
	
	if (!pFileName)
	{
		
		return NULL;
	}
	

	VSAnimSet * pAnimSet = NULL;



	VSStream Stream;
	if(!Stream.Load(pFileName))
	{
		
		return NULL;
	}

	pAnimSet = (VSAnimSet *)Stream.GetObjectByRtti(VSAnimSet::ms_Type);


	if (!pAnimSet)
	{
		VSMAC_DELETE(pAnimSet);
		
		return NULL;
	}

	
	return pAnimSet;
}
bool VSResourceManager::SaveAnimSet(VSAnimSet * pAnimSet)
{
	if (!pAnimSet)
	{
		return false;
	}
	if (!pAnimSet->GetResourceName().GetLength())
	{
		return false;
	}
	VSString FileName = ms_AnimPath + pAnimSet->GetResourceName().GetString();
	VSStream AnimSetStream;
	if(!pAnimSet->Register(AnimSetStream))
		return 0;
	FileName += _T(".") + ms_FileSuffix[FS_ANIMSET];
	if(!AnimSetStream.Save(FileName.GetBuffer()))
		return 0;
	return 1;
}
VSStaticMeshNode * VSResourceManager::LoadStaticMesh(const TCHAR *pFileName)
{
	
	if (!pFileName)
	{
		
		return NULL;
	}
	

	VSStaticMeshNode * pStaticMeshNode = NULL;

	VSStream Stream;
	if(!Stream.Load(pFileName))
	{
		
		return NULL;
	}

	pStaticMeshNode = (VSStaticMeshNode *)Stream.GetObjectByRtti(VSStaticMeshNode::ms_Type);

	if (!pStaticMeshNode)
	{
		
		return NULL;
	}

	
	return pStaticMeshNode;
}
bool VSResourceManager::SaveStaticMesh(VSStaticMeshNode * pStaticMeshNode)
{
	if (!pStaticMeshNode->GetResourceName().GetLength())
	{
		return false;
	}
	VSString FileName = ms_StaticMeshPath + pStaticMeshNode->GetResourceName().GetString();
	VSStream StaticMeshStream;
	if(!pStaticMeshNode->Register(StaticMeshStream))
		return 0;
	FileName += _T(".") + ms_FileSuffix[FS_STATIC_MODEL];
	if(!StaticMeshStream.Save(FileName.GetBuffer()))
		return 0;
	return 1;
}
VSSkelectonMeshNode * VSResourceManager::LoadSkelectonMesh(const TCHAR *pFileName)
{

	
	if (!pFileName)
	{
		
		return NULL;
	}
	

	VSSkelectonMeshNode * pSkelectonMeshNode = NULL;

	VSStream Stream;
	if(!Stream.Load(pFileName))
	{
		
		return NULL;
	}

	pSkelectonMeshNode = (VSSkelectonMeshNode *)Stream.GetObjectByRtti(VSSkelectonMeshNode::ms_Type);

	if (!pSkelectonMeshNode)
	{
		
		return NULL;
	}

	
	return pSkelectonMeshNode;
}
bool VSResourceManager::SaveSkelectonMeshNode(VSSkelectonMeshNode * pSkelectonMesh)
{
	if (!pSkelectonMesh)
	{
		return false;
	}
	if (!pSkelectonMesh->GetResourceName().GetLength())
	{
		return false;
	}
	VSString FileName = ms_SkelectionMeshPath + pSkelectonMesh->GetResourceName().GetString();
	VSStream SkelectonMeshStream;
	if(!pSkelectonMesh->Register(SkelectonMeshStream))
		return 0;
	FileName += _T(".") + ms_FileSuffix[FS_SKELECTON_MODEL];
	if(!SkelectonMeshStream.Save(FileName.GetBuffer()))
		return 0;
	return 1;
}
VSVertexFormat *VSResourceManager::LoadVertexFormat(VSVertexBuffer * pVertexBuffer,VSArray<VSVertexFormat::VERTEXFORMAT_TYPE> *pFormatArray)
{
	if(!pVertexBuffer && !pFormatArray)
		return NULL;
	if (pVertexBuffer)
	{
		if(pVertexBuffer->m_pVertexFormat)
			return pVertexBuffer->m_pVertexFormat;
	}
	
	VSArray<VSVertexFormat::VERTEXFORMAT_TYPE> FormatArray;
	if (!pFormatArray)
	{	
		if(!pVertexBuffer->GetVertexFormat(FormatArray))
			return	NULL;
		pFormatArray = &FormatArray;
	}
	else
	{
		if (!pFormatArray->GetNum())
		{
			return	NULL;
		}
	}

	unsigned int lVertexFormatCode = CRC32Compute(pFormatArray->GetBuffer(), sizeof(VSVertexFormat::VERTEXFORMAT_TYPE) * pFormatArray->GetNum());
	
	VSVertexFormat * pVertexFormat = NULL;
	pVertexFormat = (VSVertexFormat *)VSResourceManager::GetVertexFormatSet().CheckIsHaveTheResource(lVertexFormatCode);
	if(pVertexFormat)
	{
		if (pVertexBuffer)
		{
			pVertexBuffer->m_pVertexFormat = pVertexFormat;
		}
		return pVertexFormat;
	}
	pVertexFormat = VS_NEW VSVertexFormat();

	VSResourceManager::GetVertexFormatSet().AddResource(lVertexFormatCode,pVertexFormat);
	if (pVertexBuffer)
	{
		pVertexBuffer->m_pVertexFormat = pVertexFormat;
	}
	pVertexFormat->m_FormatArray = *pFormatArray;
	pVertexFormat->m_uiVertexFormatCode = lVertexFormatCode;
	pVertexFormat->LoadResource(VSRenderer::ms_pRenderer);
	return pVertexFormat;

}
VSName * VSResourceManager::CreateName(const TCHAR * pChar)
{
	if (!pChar)
	{
		return NULL;
	}
	unsigned int uiCRCCode  = CRC32Compute(pChar,(unsigned int)VSStrLen(pChar));
	VSName * pName = NULL;
	pName = VSResourceManager::GetNameSet().CheckIsHaveTheResource(uiCRCCode);
	if (!pName)
	{
		pName = VS_NEW VSName(pChar,uiCRCCode);
		if (!pName)
		{
			return NULL;
		}

		VSResourceManager::GetNameSet().AddResource(pName->m_uiID,pName);
	}

	return pName;
}
VSName * VSResourceManager::CreateName(const VSString & String)
{

	unsigned int uiCRCCode  = CRC32Compute(String.GetBuffer(),String.GetLength());
	VSName * pName = NULL;
	pName = VSResourceManager::GetNameSet().CheckIsHaveTheResource(uiCRCCode);
	if (!pName)
	{
		pName = VS_NEW VSName(String,uiCRCCode);
		if (!pName)
		{
			return NULL;
		}

		VSResourceManager::GetNameSet().AddResource(pName->m_uiID,pName);
	}

	return pName;
}
VSVShader * VSResourceManager::CreateVShader(MaterialShaderPara1 &MSPara1,unsigned int uiPassType,unsigned int uiShaderID)
{
	ms_VShaderCri.Lock();
	if (!MSPara1.pGeometry || !MSPara1.pMaterialInstance)
	{
		ms_VShaderCri.Unlock();
		return NULL;
	}
	VSMaterial * pMaterial = MSPara1.pMaterialInstance->GetMaterial();
	if (!pMaterial)
	{
		ms_VShaderCri.Unlock();
		return NULL;
	}
	VSVShader * pVShader = NULL;
	pVShader = VS_NEW VSVShader();
	if(!pVShader)
	{
		ms_VShaderCri.Unlock();
		return NULL;
	}

	VSString VShaderString;
#ifdef _DEBUG
	VSString ShaderFileName;
	if(!VSShaderStringFactory::CreateVShaderString(pVShader,MSPara1,uiPassType,uiShaderID,VShaderString,ShaderFileName))
#else
	if(!VSShaderStringFactory::CreateVShaderString(pVShader,MSPara1,uiPassType,uiShaderID,VShaderString))
#endif
	{
		ms_VShaderCri.Unlock();
		VSMAC_DELETE(pVShader);
		return NULL;
	}
#ifdef _DEBUG
	pVShader->SetResourceName(ShaderFileName);
#endif
	pVShader->SetShaderString(VShaderString,VSRenderer::GetVShaderProgramMain());
	
	VSShaderKey::SetMaterialVShaderKey(&pVShader->m_ShaderKey,MSPara1,uiPassType);
	VSRenderer::ms_pRenderer->LoadVShaderProgram(pVShader);
	ms_VShaderCri.Unlock();
	return pVShader;
}
VSPShader * VSResourceManager::CreatePShader(MaterialShaderPara1 &MSPara1,unsigned int uiPassType,unsigned int uiShaderID)
{
	ms_PShaderCri.Lock();
	if (!MSPara1.pGeometry || !MSPara1.pMaterialInstance)
	{
		ms_PShaderCri.Unlock();
		return NULL;
	}
	VSMaterial * pMaterial = MSPara1.pMaterialInstance->GetMaterial();
	if (!pMaterial)
	{
		ms_PShaderCri.Unlock();
		return NULL;
	}
	VSPShader * pPShader = NULL;
	pPShader = VS_NEW VSPShader();
	if(!pPShader)
	{
		ms_PShaderCri.Unlock();
		return NULL;
	}

	VSString PShaderString;
#ifdef _DEBUG
	VSString ShaderFileName;
	if (!VSShaderStringFactory::CreatePShaderString(pPShader,MSPara1,uiPassType,uiShaderID,PShaderString,ShaderFileName))
#else
	if (!VSShaderStringFactory::CreatePShaderString(pPShader,MSPara1,uiPassType,uiShaderID,PShaderString))
#endif
	{
		ms_PShaderCri.Unlock();
		VSMAC_DELETE(pPShader);
		return NULL;
	}
#ifdef _DEBUG
	pPShader->SetResourceName(ShaderFileName);
#endif
	pPShader->SetShaderString(PShaderString,VSRenderer::GetPShaderProgramMain());
	
	VSShaderKey::SetMaterialPShaderKey(&pPShader->m_ShaderKey,MSPara1,uiPassType);
	VSRenderer::ms_pRenderer->LoadPShaderProgram(pPShader);
	ms_PShaderCri.Unlock();
	return pPShader;
}


VSBlendState * VSResourceManager::CreateBlendState(const VSBlendDesc & BlendDesc)
{
	unsigned int uiHashCode = CRC32Compute(&BlendDesc, sizeof(VSBlendDesc));

	VSBlendState * pBlendState = NULL;
	pBlendState = (VSBlendState *)VSResourceManager::GetBlendStateSet().CheckIsHaveTheResource(uiHashCode);
	if(pBlendState)
	{

		return pBlendState;
	}
	pBlendState = VS_NEW VSBlendState();
	pBlendState->m_BlendDesc = BlendDesc;
	VSResourceManager::GetBlendStateSet().AddResource(uiHashCode,pBlendState);
	
	return pBlendState;
}
VSDepthStencilState * VSResourceManager::CreateDepthStencilState(const VSDepthStencilDesc & DepthStencilDesc)
{
	unsigned int uiHashCode = CRC32Compute(&DepthStencilDesc, sizeof(VSDepthStencilDesc));

	VSDepthStencilState * pDepthStencilState = NULL;
	pDepthStencilState = (VSDepthStencilState *)VSResourceManager::GetDepthStencilStateSet().CheckIsHaveTheResource(uiHashCode);
	if(pDepthStencilState)
	{

		return pDepthStencilState;
	}
	pDepthStencilState = VS_NEW VSDepthStencilState();
	pDepthStencilState->m_DepthStencilDesc = DepthStencilDesc;
	VSResourceManager::GetDepthStencilStateSet().AddResource(uiHashCode,pDepthStencilState);

	return pDepthStencilState;
}
VSRasterizerState * VSResourceManager::CreateRasterizerState(const VSRasterizerDesc &RasterizerDesc)
{
	unsigned int uiHashCode = CRC32Compute(&RasterizerDesc, sizeof(VSRasterizerDesc));

	VSRasterizerState * pRasterizerState = NULL;
	pRasterizerState = (VSRasterizerState *)VSResourceManager::GetRasterizerStateSet().CheckIsHaveTheResource(uiHashCode);
	if(pRasterizerState)
	{

		return pRasterizerState;
	}
	pRasterizerState = VS_NEW VSRasterizerState();
	pRasterizerState->m_RasterizerDesc = RasterizerDesc;
	VSResourceManager::GetRasterizerStateSet().AddResource(uiHashCode,pRasterizerState);

	return pRasterizerState;
}
VSSamplerState * VSResourceManager::CreateSamplerState(const VSSamplerDesc &SamplerDesc)
{
	unsigned int uiHashCode = CRC32Compute(&SamplerDesc, sizeof(VSSamplerDesc));

	VSSamplerState * pSamplerState = NULL;
	pSamplerState = (VSSamplerState *)VSResourceManager::GetSamplerStateSet().CheckIsHaveTheResource(uiHashCode);
	if(pSamplerState)
	{

		return pSamplerState;
	}
	pSamplerState = VS_NEW VSSamplerState();
	pSamplerState->m_SamplerDesc = SamplerDesc;
	VSResourceManager::GetSamplerStateSet().AddResource(uiHashCode,pSamplerState);

	return pSamplerState;
}
VSRenderTarget * VSResourceManager::CreateRenderTarget(unsigned int uiWidth, unsigned int uiHeight,unsigned int uiFormatType,
									unsigned int uiMulSample)
{
	VSRenderTarget *Temp = VS_NEW VSRenderTarget(uiWidth,uiHeight,uiFormatType,uiMulSample);
	GetRenderTargetArray().AddResource(Temp);
	return Temp;
}
VSRenderTarget * VSResourceManager::CreateRenderTarget(VSTexture * pCreateBy,unsigned int uiMulSample
									,unsigned int uiLevel,unsigned int uiFace)
{
	if (pCreateBy->IsCompress())
	{
		return NULL;
	}
	VSMAC_ASSERT(uiMulSample < VSRenderer::MS_MAX)
	if (uiMulSample >= VSRenderer::MS_MAX)
	{
		return NULL;
	}
	VSMAC_ASSERT(uiLevel < pCreateBy->GetMipLevel());
	if (uiLevel >= pCreateBy->GetMipLevel())
	{
		return NULL;
	}
	if (uiFace >= VSCubeTexture::F_MAX)
	{
		return NULL;
	}
	VSRenderTarget *Temp = VS_NEW VSRenderTarget(pCreateBy,uiMulSample,uiLevel,uiFace);
	GetRenderTargetArray().AddResource(Temp);
	return Temp;
}
VSDepthStencil * VSResourceManager::CreateDepthStencil(unsigned int uiWidth,unsigned int uiHeight,
									unsigned int uiMulSample,unsigned int uiFormatType)
{
	VSDepthStencil *Temp = VS_NEW VSDepthStencil(uiWidth,uiHeight,uiMulSample,uiFormatType);
	GetDepthStencilArray().AddResource(Temp);
	return Temp;
}